//package org.jwcloud.project.module.zx.common;
//
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.jw.cj.data.entity.AbstractSelectCondition;
//
//import com.jw.cj.feign.RemoteMethodInvocation;
////import org.domain.security.OrgModel;
////import org.jwcloud.security.auth.feign.SystemService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.util.ObjectUtils;
//import org.springframework.util.StringUtils;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
///**
// * @author wangtiefu
// * @description
// * @date 2020/2/22
// */
//@Component
//public class UserInfoUtils {
//
//    public  final static String FORM_DATA = "form-data";
//
//    public final static String GENERAL_USER = "general";
//
//    public final static String ADMIN_USER = "admin";
//
//    @Autowired
//    public RemoteMethodInvocation remoteMethodInvocation;
//
//    @Autowired
//    public UserInfoUtils userInfoUtils;
//
//    /**
//     * 获取用户详细信息
//     * @return
//     */
//    public Map<String , Object> getUserMap(){
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        Map<String , Object> userMap = (Map<String , Object>) request.getAttribute("user");
//        return userMap;
//    }
//
//    public String getUserId(){
//        return getUserMap().get("id").toString();
//    }
////    /**
////     * 获取用户查询条件
////     */
////    public void getUserConditions(AbstractSelectCondition selectCondition){
////        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
////        String userType = request.getHeader("user-type");
////        /*String orgId = userInfoUtils.getOwnOrgId(); //  新增的
////        String userId = userInfoUtils.getUserId(); //  新增的
////        selectCondition.setUserId(userId); //  新增的
////        selectCondition.setOrgId(orgId); //  新增的*/
////        switch (userType){
////            case GENERAL_USER:
////                selectCondition.setUserId(getUserId());
////                selectCondition.setOrgId(getOwnOrgId());
////                break;
////            case ADMIN_USER:
////                selectCondition.setOrgIds(getOrgIds());
////                break;
////        }
////    }
//
//    /**
//     * 获取专属org信息
//     * @return
//     */
//    public JSONObject getOwnOrg(){
//        JSONArray array = getOrg();
//
//        Object parentId = array.getJSONObject(0).get("parentId");
//        if(ObjectUtils.isEmpty(parentId)){
//            return array.getJSONObject(0);
//        }
//        JSONObject lastOrg = array.getJSONObject(0);
//        outer:while(true){
//            for (int index = 0 , length = array.size() ; index < length ; index++) {
//                String id = array.getJSONObject(index).get("id").toString();
//                if(id.equals(parentId.toString())){
//                    parentId = array.getJSONObject(index).get("parentId");
//                    if(ObjectUtils.isEmpty(parentId)){
//                        return array.getJSONObject(index);
//                    }
//                    lastOrg = array.getJSONObject(index);
//                    continue outer;
//                }
//            }
//            break ;
//        }
//        return lastOrg;
//    }
//
//    /**
//     * 获取用户专属信息
//     * @return
//     */
//    public String getOwnOrgId(){
//        return getOwnOrg().get("id").toString();
//    }
//
//    /**
//     * 获取用户所有orgId
//     * @return
//     */
//    public List<String> getOrgIds(){
//        JSONArray array = getOrg();
//        List<String> orgIds = new ArrayList<>();
//
//        for(int index = 0 , length = array.size() ; index < length ; index++){
//            orgIds.add(array.getJSONObject(index).get("id").toString());
//        }
//
//        return orgIds;
//    }
//
//    /**
//     * 有多媒体文件请求是获取org详细信息
//     * @return
//     */
//    public JSONArray getOrg() {
//
//        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
////        String contentType = request.getHeader("Content-Type");
////
////        if (StringUtils.isEmpty(contentType)) {
////            throw new RuntimeException("当前请求没有Content-Type信息.");
////        }
////        JSONArray array = null;
////
////        if (contentType.contains(FORM_DATA)) {
////            JSONObject orgJson = systemService.findOrgWithFormDataHeaders();
////            System.out.println(orgJson);
////            if (!"200".equals(orgJson.get("code"))) {
////                throw new RuntimeException("当前用户没有关联任何机构.");
////            }
////            array = orgJson.getJSONArray("data");
////        } else {
////
////            if (request.getMethod().equalsIgnoreCase("post")
////                    ||request.getMethod().equalsIgnoreCase("put")) {
////
////                JSONObject orgJson = systemService.findOrg();
////                if (!"200".equals(orgJson.get("code"))) {
////                    throw new RuntimeException("当前用户没有关联任何机构.");
////                }
////                array = orgJson.getJSONArray("data");
////            }
////            if (request.getMethod().equalsIgnoreCase("get") || request.getMethod().equalsIgnoreCase("delete")) {
////                JSONObject orgJson = systemService.getOrg();
////                if (!"200".equals(orgJson.get("code"))) {
////                    throw new RuntimeException("当前用户没有关联任何机构.");
////                }
////                array = orgJson.getJSONArray("data");
////            }
////        }
//        JSONArray array = null;
//        try {
//            array = remoteMethodInvocation.getOrg(request);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return array;
//    }
//
//}
